package com.example.administrator.myapplication.View.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.myapplication.Bean.User;
import com.example.administrator.myapplication.Http.Api;
import com.example.administrator.myapplication.Http.MyGlideModel;
import com.example.administrator.myapplication.Http.RetrofitHelper;
import com.example.administrator.myapplication.Presenter.LoginPresenter;
import com.example.administrator.myapplication.Presenter.MinePresenter;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.Util.SharedPreferencesUtil;
import com.example.administrator.myapplication.View.Fragment.IMineView;

import java.io.IOException;

import okhttp3.ResponseBody;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener,ILoginView {
    private EditText et_username;
    private EditText et_pwd;
    private EditText et_captcha;
    private TextView tv_logon;
    private TextView tv_login;
    private TextView tv_forget;
    private ImageView iv_captcha;
    private LoginPresenter loginPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            getWindow().setStatusBarColor(Color.parseColor("#6A9C9D"));
        }
        initView();
    }

    public void  initView(){
        et_username = findViewById(R.id.et_username);
        et_pwd = findViewById(R.id.et_pwd);
        et_captcha = findViewById(R.id.et_captcha);
        tv_logon = findViewById(R.id.tv_logon);
        tv_login = findViewById(R.id.tv_login);
        tv_forget = findViewById(R.id.tv_forget);
        iv_captcha = findViewById(R.id.iv_captcha);
        loginPresenter = new LoginPresenter(this);
        tv_login.setOnClickListener(this);
        tv_logon.setOnClickListener(this);
        iv_captcha.setOnClickListener(this);
        loadCaptcha();
    }

    private int checkInput(){
        int count = 0;
        if (TextUtils.isEmpty(et_username.getText().toString())){
            et_username.setError("用户名不能为空");
            count++;
        }
        if(TextUtils.isEmpty(et_pwd.getText().toString())){
            et_pwd.setError("用户名不能为空");
            count++;
        }
        if(TextUtils.isEmpty(et_captcha.getText().toString())){
            et_captcha.setError("验证码不能为空");
            count++;
        }
        return count;
    }

    private void login(){
        /**
         * 输入框检查
         * */
        if(checkInput()!=0)
            return;
        /**
         * 调用登录方法
         * */
        String username = et_username.getText().toString();
        String pwd = et_pwd.getText().toString();
        String captcha_code = et_captcha.getText().toString();
        loginPresenter.login(username,pwd,captcha_code,false);
    }

    private void loadCaptcha(){
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.drawable.captcha_loading)
                .error(R.drawable.captcha_loadfail)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true);
        Glide.with(this)
                .load(Api.Captcha_Url)
                .apply(requestOptions)
                .into(iv_captcha);
    }

    @Override
    public void showError(String error) {
        loadCaptcha();
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showSuccess(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
        Intent it=new Intent(getApplicationContext(),MainActivity.class);//启动MainActivity
        startActivity(it);
        finish();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_captcha:
                et_captcha.setText("");
                et_pwd.setText("");
                loadCaptcha();
                break;
            case R.id.tv_login:
                login();
                break;
            case R.id.tv_logon:
                Intent it=new Intent(getApplicationContext(),LogonActivity.class);//启动MainActivity
                startActivity(it);
        }
    }


}
